package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.dto.BusinessPoolDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * ClassName: BusinessController
 * Package: com.qk.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/8/2 20:16
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping(("/businesses"))
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    /*
     * 添加商机
     */
    @PostMapping
    public Result add(@RequestBody Business business) {
        log.info("添加商机：{}", business);
        business.setStatus(1);//待分配
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }

    /*
     * 获取商机列表
     */
    @GetMapping
    public Result list(BusinessDto businessDto) {
        log.info("获取商机列表");
        PageResult<Business> pageResult = businessService.listBusinesses(businessDto);
        return Result.success(pageResult);
    }

    /*
    分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机：{}, {}", businessId, userId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);//待跟进
        business.setUpdateTime(LocalDateTime.now());

        businessService.updateById(business);
        return Result.success();
    }

    /*
    根据ID查询（查询回显）
     */
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("根据id查询商机:{}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }

    /*
    跟进商机
     */
    @PutMapping
    public Result trackBusiness(@RequestBody Business business) {
        log.info("跟进商机 {}" ,  business);
        businessService.trackBusiness(business);
        return Result.success();
    }

    /*
    踢回公海
    修改商机的状态，将该商机的状态修改为 4
     */
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id) {
        log.info("踢回公海：{}", id);
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);//公海
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }

    /*
    转客户处理
     */
    @PostMapping("toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("转客户处理：{}", id);
        businessService.toCustomer(id);
        return Result.success();
    }

    /*
    公海池列表查询
     */
    @GetMapping("/pool")
    public Result businessPool(BusinessPoolDto businessPoolDto) {
        log.info("公海池列表查询：{}", businessPoolDto);
        PageResult<Business> pageResult = businessService.listBusinessesPool(businessPoolDto);
        return Result.success(pageResult);
    }



}
